package com.bothin.experiment4.controller;

import com.bothin.experiment4.constant.NoticeNumConstant;
import com.bothin.experiment4.dto.AnalyDTO;
import com.bothin.experiment4.dto.ContactDTO;
import com.bothin.experiment4.dto.StatisticDTO;
import com.bothin.experiment4.dto.SumaryDTO;
import com.bothin.experiment4.security.LoginUser;
import com.bothin.experiment4.security.LoginUserUtil;
import com.bothin.experiment4.service.IContactDetailService;
import com.bothin.experiment4.service.IContactService;
import com.bothin.experiment4.service.IProductFormService;
import com.bothin.experiment4.service.IStatisticService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by bothin
 * At 2019/12/21 20:49
 * Description:
 */
@Controller
@RequestMapping("/salesman")
@Secured("ROLE_USER_SALESMAN")
public class SalesmanController {
    @Autowired
    private IStatisticService statisticService;
    @Autowired
    private IContactService contactService;
    @Autowired
    private IContactDetailService contactDetailService;
    @Autowired
    private IProductFormService productFormService;

    @GetMapping("/contact/list")
    @ResponseBody
    public ModelAndView contact_list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "size", defaultValue = "8") Integer size,
                                     @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                    Map<String, Object> map){
        PageHelper.startPage(page,size);
        List<ContactDTO> contactDTOS = null;
        if (!StringUtils.isEmpty(keyword)){
            contactDTOS = contactService.findByEmployeeIdAndKeywordDTO(LoginUserUtil.getLoginUser().getEmployeeId(),keyword);
            map.put("key","&keyword="+keyword);
        }else {
            contactDTOS = contactService.findByEmployeeIdDTO(LoginUserUtil.getLoginUser().getEmployeeId());
        }
        PageInfo<ContactDTO> pageInfo = new PageInfo<>(contactDTOS);
        map.put("pageInfo",pageInfo);
        return new ModelAndView("/contact/salesman_contact_list",map);
    }

    @RequestMapping("/contact/to/detail")
    public ModelAndView toDetail(@RequestParam(value = "contactId") String contactId,
                                 Map<String, Object> map){
        ContactDTO contactDTO = contactService.findByIdDTO(contactId);
        contactDTO.setContactDetailDTOList(contactDetailService.findByContactId(contactId));
        contactDTO.setProductFormDTOList(productFormService.findByContactIdDTO(contactId));
        map.put("contactDTO", contactDTO);
        return new ModelAndView("contact/salesman_contact_detail",map);
    }

    @RequestMapping("/to/analy")
    public ModelAndView to_analy(){
        return new ModelAndView("/statistics/salesman_analy");
    }

    @RequestMapping("/analy")
    @ResponseBody
    public List<SumaryDTO> analy(@RequestParam(value = "start",defaultValue = "") String start,
                                 @RequestParam(value = "end",defaultValue = "") String end){
        if (StringUtils.isEmpty(start)||StringUtils.isEmpty(end)){
            return statisticService.getTheSumEachMouthOfSalesman(NoticeNumConstant.TEST_YEAR,LoginUserUtil.getLoginUser().getEmployeeId()).stream()
                    .peek(e->e.setLabel(e.getLabel()+"月")).collect(Collectors.toList());
        }else {
            List<SumaryDTO> temp = new ArrayList<>();
            temp.add(statisticService.getOneSalesmanSStatisticById(start,end,LoginUserUtil.getLoginUser().getEmployeeId()));
            return temp;
        }
    }
}
